package com.doit.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.RegionLocator;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

public class HfileBuilder {
    public static class HfileMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>{
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>.Context context) throws IOException, InterruptedException {
            String s = value.toString();
            String[] arr = s.split(",");
            context.write(new ImmutableBytesWritable(Bytes.toBytes(arr[0])), new KeyValue(Bytes.toBytes(arr[0]), Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes(arr[1])));

            context.write(new ImmutableBytesWritable(Bytes.toBytes(arr[0])), new KeyValue(Bytes.toBytes(arr[0]), Bytes.toBytes("f1"), Bytes.toBytes("age"), Bytes.toBytes(arr[2])));

            context.write(new ImmutableBytesWritable(Bytes.toBytes(arr[0])), new KeyValue(Bytes.toBytes(arr[0]), Bytes.toBytes("f1"), Bytes.toBytes("gender"), Bytes.toBytes(arr[3])));
        }
    }

    public static void main(String[] args) throws Exception {
        System.setProperty("HADOOP_USER_NAME","root");

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux01:2181");

        Job job = Job.getInstance(conf);

        //设置主类
        job.setJarByClass(HfileBuilder.class);

        //设置mapper类
        job.setMapperClass(HfileMapper.class);

        //设置map的输出类型
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(KeyValue.class);

        //设置reducetask的个数
        job.setNumReduceTasks(0);

        //还要给hbase表中的具体哪个信息
        Connection conn = ConnectionFactory.createConnection(conf);
        Table table = conn.getTable(TableName.valueOf("test:bulk"));
        //region的加载器
        RegionLocator regionLocator = conn.getRegionLocator(TableName.valueOf("test:bulk"));
        HFileOutputFormat2.configureIncrementalLoad(job,table,regionLocator);

        //还没有结束哦，既然你需要的是生成hfile文件，那么就需要获取到hfile具体的格式
        job.setOutputFormatClass(HFileOutputFormat2.class);

        //设置文件输入输出的路劲
        FileInputFormat.setInputPaths(job,new Path("data/test.txt"));
        FileOutputFormat.setOutputPath(job,new Path("hdfs://linux01:8020/hfile_output/"));

        //启动程序
        job.waitForCompletion(true);



    }
}
